//自定义实现阻塞队列
class MyBlockingQueue{
    private int[] items = new int[1000];
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    public void put(int value) throws InterruptedException {
        synchronized (this) {
            while (size == items.length) {
                this.wait();
            }
            items[tail] = value;
            tail++;
            if (tail >= items.length) {
                tail = 0;
            }
            size++;
            this.notify();
        }
    }

    public Integer take() throws InterruptedException {
        synchronized (this) {
            while (size == 0) {
                this.wait();
            }
            int result = items[head];
            head++;
            if (head >= items.length) {
                head = 0;
            }
            size--;
            this.notify();
            return result;
        }
    }
}

public class demo6 {
    public static void main(String[] args) throws InterruptedException {
      MyBlockingQueue queue = new MyBlockingQueue();
      Thread customer = new Thread(() -> {
          while (true){
              try {
                  int result = queue.take();
                  System.out.println("消费" + result);
              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }
          }
      });
      customer.start();
        Thread producer = new Thread(() -> {
            int count = 0;
            while (true){
                try {
                    System.out.println("生产" + count);
                    queue.put(count);
                    count++;
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        producer.start();
    }
}






















